interface Todo {
  readonly title: string;
  completed: boolean;
  description: string;
  date?:Date
  publisher?:string  // 发表人
}

// 属性都变为必选项
type Required1<T> = {
  [K in keyof T]-?: T[K]
}
type Todo1 = Required<Todo>

// 属性都变为可选项
type Partial1<T> = {
  // [K in keyof T]?: T[K]
  [K in keyof T]+?: T[K]
}
type Todo2 = Partial<Todo>

// 属性都变为只读
type Readonly1<T> = {
  +readonly [K in keyof T]: T[K]
}
type Todo3 = Readonly<Todo>

// 属性都去掉只读
type NoReadonly<T> = {
  -readonly [K in keyof T]: T[K]
}
type Todo4 = NoReadonly<Todo>


// ### 作业：实现一个getSubItemsFromArr的函数，传入数组，后面参数传递要过滤出来的属性。
//           返回的新数组中，每一个对象只有我们要的那几个属性，在forEach中能提示对象属性。

const arr = [
  { student: "llw", age: 27, teacher: "yws", relationship: "SecretLovePeriod" },
  { student: "llw", age: 27, teacher: "ljl", relationship: "SecretLovePeriod" },
  { student: "llw", age: 27, teacher: "lq", relationship: "SecretLovePeriod" },
  { student: "llw", age: 27, teacher: "lyy", relationship: "SecretLovePeriod" },
]

// 1、用2个循环实现
function getSubItemsFromArr1<T extends  Record<string, any>, K extends keyof T>(arr: Array<T>, ...args: Array<K>): Array<Pick<T, K>> {
  let res: Array<Pick<T, K>> = []
  arr.forEach(item => {
    let obj = {} as Pick<T, K>
    args.forEach(key => {
      obj[key] = item[key]
    })
    res.push(obj)
  })
  return res
}

// 最优解
function getSubItemsFromArr<T, K extends keyof T>(
  arr: T[],
  ...keys: K[]
): Array<Pick<T, K>> {
  return arr.map(item => 
    keys.reduce((acc, key) => {
      acc[key] = item[key];
      return acc;
    }, {} as Pick<T, K>)
  );
}

getSubItemsFromArr(arr, 'student', 'teacher').forEach(item => {
  console.log(item.student)
})
export {}